#include "radiosity_panel.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>

#include <iostream>

namespace Ui {
	
Radiosity_Panel::Radiosity_Panel(QWidget* _parent) : QWidget(_parent) , radiosity(0) ,samples(100) {
	QVBoxLayout* panel = new QVBoxLayout();
	QHBoxLayout* samples_layout = new QHBoxLayout();
	QHBoxLayout* solving_times_layout = new QHBoxLayout();
	QHBoxLayout* calculate_layout = new QHBoxLayout();
	
	QLabel* samples_label = new QLabel(tr("Samples:"));
	samples_spinbox = new QSpinBox();;
	samples_spinbox->setRange(1,10000);
	samples_spinbox->setValue(100);
	
	samples_layout->addWidget(samples_label);
	samples_layout->addStretch();
	samples_layout->addWidget(samples_spinbox);
	
	QLabel* solving_times_label = new QLabel(tr("Solving times:"));
	solving_times_spinbox = new QSpinBox();;
	
	solving_times_layout->addWidget(solving_times_label);
	solving_times_layout->addStretch();
	solving_times_layout->addWidget(solving_times_spinbox);
	solving_times_spinbox->setRange(1,10000);
	solving_times_spinbox->setValue(30);
	
	shadow_checkbox = new QCheckBox(tr("Shadow testing"),this);
	
	calculate_button = new QPushButton(tr("Calculate Radiosity"),this);
	
	calculate_layout->addStretch();
	calculate_layout->addWidget(calculate_button);
	calculate_layout->addStretch();
	
	panel->addLayout(samples_layout);
	panel->addLayout(solving_times_layout);
	panel->addWidget(shadow_checkbox);
	panel->addLayout(calculate_layout);
	panel->addStretch();
	
	setLayout(panel);
	
	connect(samples_spinbox,SIGNAL(valueChanged(int)),this,SLOT(samples_changed(int)));
	connect(solving_times_spinbox,SIGNAL(valueChanged(int)),this,SLOT(solving_times_changed(int)));
	connect(shadow_checkbox,SIGNAL(stateChanged(int)),this,SLOT(shadow_toggled(int)));
	connect(calculate_button,SIGNAL(clicked( bool )),this,SLOT(calculate_activated(void)));
}
	
Radiosity_Panel::~Radiosity_Panel(void) {
	
}
	
void Radiosity_Panel::set_radiosity(t2c2::Radiosity* _radiosity) {
	radiosity = _radiosity;
}

t2c2::Radiosity* Radiosity_Panel::get_radiosity(void) const {
	return radiosity;
}


void Radiosity_Panel::samples_changed(int _samples) {
	if(radiosity != 0) samples = _samples;
}

void Radiosity_Panel::solving_times_changed(int _solving_times) {
	if(radiosity != 0) radiosity->set_solving_times(_solving_times);
}

void Radiosity_Panel::shadow_toggled(int _shadow) {
	if(radiosity != 0) radiosity->set_shadow(_shadow);
}

void Radiosity_Panel::calculate_activated(void) {
	if(radiosity != 0) {
		radiosity->calculate(radiosity->get_solving_times(),samples);
		emit radiosity_done();
	}
}

	
	
}
